package srzy.module

import srzy.common.ShellExecute
import srzy.common.Logging
import srzy.socket.DCCMsgActor
import srzy.diameter._
import scala.collection.mutable.ListBuffer
import srzy.socket.RBServerMsgActor
import srzy.socket.RBMsgActor
import srzy.dao.DBUtil

object DCCMsgDeal extends AbsCaseDeal with Logging {

  val queue = srzy.socket.RBMsgQueue()
  val cacheNum = Integer.parseInt(srzy.common.Config.getValue("root", "config", "CacheNum"))

  def deal = {
    val l = getCaseInfo.sendDCCMsg
    val realReceFromSMMsgs = ListBuffer.empty[String]
    val realSendToSMMsgs = ListBuffer.empty[String]

    if (queue.size() > 0) {
      logger.error("rb msg not regular")
      logger.error("queue:" + queue)
      queue.clear()
    }

    val realReceDCCMsgs = new ListBuffer[String]

    CaseDealContext.sessionId = getCaseInfo.sessionId

    getCaseInfo.actions.foreach { x =>
      x match {
        case v: SendAction =>
          //          logger.debug("sendMsg:" + v.send.toFormatStr)
          DCCMsgActor() ! v.send
        case v: ReceAction =>
          val rece = (DCCMsgActor() !! 'getRece).apply.toString
          //          logger.debug("receMsg:" + rece)
          realReceDCCMsgs.append(rece)
        case v: WaitAction =>
          logger.debug("WaitAction:" + v.waitTime)
          Thread.sleep(v.waitTime)
        case v: DbOperAction =>
          logger.debug("DbOperAction:" + v.source)
          val dbUtil = v.dbUtil
          dbUtil.executeUpdate(v.sqlList)
          if (v.source == "SMOracleSource") {
            RefreshSM.refresh(v.sqlList)
          }
        case v: CacheOperAction =>
          logger.debug("CacheOperAction")
          srzy.smcache.CacheUtil.operCache(cacheNum, v.sqlList)
        case v: CleanCacheAction =>
          logger.debug("CleanCacheAction")
          srzy.smcache.CacheUtil.cleanCache(cacheNum)
        case v: ShellOperAction =>
          logger.debug("ShellOperAction:" + v.shell)
          if (v.shell != "") {
            new ShellExecute {
              val shell = v.shell
            }.execute
          }
        case v: DCCTimeoutAction =>
          logger.debug("DCCTimeoutAction:" + v.time)
          DCCMsgActor() ! ('timeout, v.time)
        case v: RefreshSMAction =>
          logger.debug("RefreshSMAction")
          RefreshSM.refresh()

        case v: srzy.module.RBMsgSleepAction =>
          logger.debug("RBMsgSleepAction")
          srzy.socket.RBMsgActionQueue().add(new srzy.socket.RBMsgSleepAction {
            val time = 6000
          })
        //          Thread.sleep(6000)
        case v: srzy.module.RBMsgSleepAndDiscardAction =>
          logger.debug("RBMsgSleepAndDiscardAction")
          srzy.socket.RBMsgActionQueue().add(new srzy.socket.RBMsgSleepAndDiscardAction {
            val time = 6000
          })
        //          Thread.sleep(6000)
      }
    }

    CaseDealContext.sessionId = ""

    getCaseInfo.realReceDCCMsg = realReceDCCMsgs.toList

    //    l.map(send => {
    //      logger.debug("sendMsg:" + send.toFormatStr)
    //      DCCMsgActor() ! send
    //      val rece = (DCCMsgActor() !! 'getRece).apply.toString
    //      logger.debug("receMsg:" + rece)
    //      rece
    //    })

    Thread.sleep(1000L)
    var flag = true
    while (flag) {
      val msg = queue.poll
      if (msg == null) {
        flag = false
      } else {
        msg match {
          case ('SMToRB, str: String) => realReceFromSMMsgs.append(str)
          case ('RBToSM, str: String) => realSendToSMMsgs.append(str)
        }
      }
    }

    getCaseInfo.realReceFromSMMsg = realReceFromSMMsgs.toList
    getCaseInfo.realSendToSMMsg = realSendToSMMsgs.toList

  }
}